package com.jeesite.modules.ic.web;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.ic.dao.IcUserDao;
import com.jeesite.modules.ic.entity.IcCompany;
import com.jeesite.modules.ic.entity.IcUser;
import com.jeesite.modules.ic.service.IcUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ic.entity.IcCard;
import com.jeesite.modules.ic.service.IcCardService;

/**
 * 流量卡Controller
 * @author 李寒冰
 * @version 2024-06-19
 */
@Controller
@RequestMapping(value = "${adminPath}/ic/icCard")
public class IcCardController extends BaseController {

	@Autowired
	private IcCardService icCardService;

	@Autowired
	private IcUserService icUserService;

	@Autowired
	private IcUserDao icUserDao;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public IcCard get(String id, boolean isNewRecord) {
		return icCardService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("ic:icCard:view")
	@RequestMapping(value = {"list", ""})
	public String list(IcCard icCard, Model model) {
		model.addAttribute("icCard", icCard);
		return "modules/ic/icCardList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ic:icCard:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<IcCard> listData(IcCard icCard, HttpServletRequest request, HttpServletResponse response) {
		icCard.setPage(new Page<>(request, response));
		Page<IcCard> page = icCardService.findPage(icCard);
		List<IcCard> list = page.getList();
		for (int i = 0; i < list.size(); i++) {
			IcCard icCard1 = list.get(i);
			String userId = icCard1.getUserId();
			if(StringUtils.isNotEmpty(userId)){
				IcCompany userCompany = icUserDao.getUserCompany(userId);
				if(userCompany!=null){
					icCard1.setCompanyName(userCompany.getName());
				}
			}
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ic:icCard:view")
	@RequestMapping(value = "form")
	public String form(IcCard icCard, Model model) {
		model.addAttribute("icCard", icCard);

		List<IcUser> icUsers = icUserService.findList(new IcUser());
		model.addAttribute("userList", icUsers);

		return "modules/ic/icCardForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("ic:icCard:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated IcCard icCard) {
		String iccid = icCard.getIccid();
		IcCard icCardSearch = new IcCard();
		icCardSearch.setIccid(iccid);
		List<IcCard> list = icCardService.findList(icCardSearch);
		if(list.size()>0 && icCard.getId() == null){
			return renderResult(Global.FALSE, text("流量卡已存在！"));
		}
		if(icCard.getId()==null){
			icCardService.getCardInfo(icCard);
		}
		icCardService.save(icCard);
		return renderResult(Global.TRUE, text("保存流量卡成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("ic:icCard:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(IcCard icCard) {
		icCard.setStatus(IcCard.STATUS_DISABLE);
		icCardService.updateStatus(icCard);
		return renderResult(Global.TRUE, text("停用流量卡成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("ic:icCard:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(IcCard icCard) {
		icCard.setStatus(IcCard.STATUS_NORMAL);
		icCardService.updateStatus(icCard);
		return renderResult(Global.TRUE, text("启用流量卡成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("ic:icCard:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(IcCard icCard) {
		icCardService.delete(icCard);
		return renderResult(Global.TRUE, text("删除流量卡成功！"));
	}
	
}